package DAL;

import BLL.bConfig;
import DTO.dConfig;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;

import java.sql.Statement;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author nvc11190
 */
public class MyConnect {
    //Khai báo biến
    
    public Vector vTbl;
    public Vector vRow;
    public Vector vCol;
    private String host ="";
    private String port="";
    private String userName="";
    private String passWord="";
    private String database="";
    private Connection cn = null;
    private Statement st = null;
    private ResultSet rs = null;
    public  dConfig dcon ;
    public boolean flag = false;
 
    //Load Config
    void loadConfig(){
        bConfig bC =new bConfig();
        dConfig dC = bC.GetConfig();
        host = dC.getHost();
        port = dC.getPort();
        userName = dC.getUserName();
        passWord = dC.getPassWord();
        database = dC.getDatabase();
        
    }
    //Hàm Test Driver
    public void testDriver(){
        loadConfig();
        try {
            Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(MyConnect.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    public Connection getConnect(){
        if(this.cn == null){
            testDriver();
            String url = "jdbc:sqlserver://" + this.host + ":" + this.port + ";databaseName=" + this.database;
            try {
                 this.cn = DriverManager.getConnection(url, this.userName, this.passWord);
                flag=true;
            } catch (SQLException ex) {
                Logger.getLogger(MyConnect.class.getName()).log(Level.SEVERE, null, ex);
            flag=false;   
            }
            
        }
        return this.cn;
    }
            
    //Hàm Test Connection
    public boolean testConnection(){
        getConnect();
        if (flag == true){
            return true;
        }
        else return false;
        }
    
    public Statement getStatement() throws SQLException{
        if(this.st == null || this.st.isClosed()){
        this.st = (Statement) getConnect().createStatement();
        
    }
        return this.st;
    }
    
    public ResultSet excuteQuery(String Query) {
        try {
            this.rs = getStatement().executeQuery(Query);
        } catch (SQLException ex) {
            Logger.getLogger(MyConnect.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        return this.rs;
    }
    
    public int executeUpdate(String Query) throws SQLException{
        int res = Integer.MIN_VALUE;
        try {
            res = getStatement().executeUpdate(Query);
        } catch (SQLException ex) {
            Logger.getLogger(MyConnect.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            this.Close();
        }
        return res;
    }
       
     public void Close() throws SQLException{
         if (this.rs !=null){
             if(!this.rs.isClosed()){
                 this.rs.close();
             }
             this.rs = null;
         }
         if (this.st !=null){
             if(!this.st.isClosed()){
                 this.st.close();
             }
             this.st = null;
         }
         if (this.cn !=null){
             if(!this.cn.isClosed()){
                 this.cn.close();
             }
             this.cn = null;
         }
     }
    
 
    
}
